package com.yyk.yykywd.mvvm.view_model

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.orhanobut.logger.Logger
import com.yyk.yykywd.bean.BaseBean
import com.yyk.yykywd.listToString
import com.yyk.yykywd.mvvm.entity.FollowTypeBean
import com.yyk.yykywd.mvvm.model.FollowModel
import com.yyk.yykywd.mvvm.model.GetBase
import com.yyk.yykywd.mvvm.model.MaterialModel

/**
 * 文件描述：
 * Created by guo on 2019-11-18.
 */
class AddPictureViewModel : ViewModel() {
    var from: String = ""
    var id: String = ""
    //代理站点id
    var pact_id: String = ""
    //代理站点名称
    var name = ""
    //还能退款的合同数量
    var refund_num = 0
    //还能添加回款的合同数量
    var back_money_num = 0
    //合同类型
    var flag = 0
    //合同是否补全过
    var transition = 0
    //仍需回款金额
    var rxhk_money = 0
    var money: String = ""

    var delSingleOrMulti = ""

    var isFront: Boolean = true
    var type: String = ""
    var del_type: String = ""
    var del_position: Int = 0
    //LiveData,使该数据可以被感知
    var responseMessage = MutableLiveData<String>()
    //发布素材
    var mPublishMaterial = MutableLiveData<BaseBean>()
    var delSuccess = MutableLiveData<BaseBean>()
    var refundInfo = MutableLiveData<BaseBean>()
    var completionContractSuccess = MutableLiveData<BaseBean>()
    //退款申请表
    var refundForm = mutableListOf<String>()
    //退款确认单
    var refundConfirmationForm = mutableListOf<String>()
    //素材图片
    var imgs = mutableListOf<String>()
    private val materialModel: MaterialModel = MaterialModel()
    private val followModel = FollowModel()

    //合同图片
    var contract_imgs = mutableListOf<String>()
    //医院施工调研表
    var survey_form_imgs = mutableListOf<String>()
    //医疗机构许可证
    var permit_imgs = mutableListOf<String>()
    //代理商企业营业执照
    var agent_license_imgs = mutableListOf<String>()
    //身份证正面照片
    var card_front_imgs = ""
    //身份证背面照片
    var card_back_imgs = ""

    //获取站点类型以及合同类型成功
    var getTypeSuccess = MutableLiveData<FollowTypeBean>()
    //站点类型
    var siteType = mutableListOf<FollowTypeBean.SiteData>()
    //站点类型position
    var sitePosition: Int = 0
    //合同类型
    var contractType = mutableListOf<FollowTypeBean.PactData>()
    //合同类型position
    var contractTypePosition = 0
    var uploadSuccess = MutableLiveData<BaseBean>()

    //收据照片
    var receipt = mutableListOf<String>()

    fun uploadContractImg(type: String, img: String) {
        materialModel.uploadContractImg(type, img) { _, message ->
            responseMessage.postValue(message)
        }
    }

    fun removeImg(img: String) {
        materialModel.delImg(img, object : GetBase {
            override fun getInfo(base: BaseBean) {
                delSuccess.value = base
            }

            override fun onError(e: String) {

            }

        })
    }

    fun refund(cause: String, desc: String) {
        materialModel.refund(pact_id, id,
                money,
                listToString(refundForm),
                listToString(refundConfirmationForm),
                cause, desc, object : GetBase {
            override fun getInfo(base: BaseBean) {
                refundInfo.value = base
            }

            override fun onError(e: String) {

            }

        })
    }


    fun publishMaterial(content: String) {
        materialModel.publishMaterial(content, listToString(imgs), object : GetBase {
            override fun getInfo(base: BaseBean) {
                mPublishMaterial.value = base
            }

            override fun onError(e: String) {
            }
        })
    }

    fun getType() {
        followModel.getType(object : FollowModel.GetFollowType {
            override fun getInfo(type: FollowTypeBean) {
                siteType.clear()
                contractType.clear()
                siteType.addAll(type.site_data)
                contractType.addAll(type.pact_data)
                getTypeSuccess.value = type
            }

            override fun onError(e: String) {
            }

        })
    }

    fun uploadContract(order_code: String,
                       handsel_price: String,
                       agency_name: String) {
        materialModel.uploadContract(from, id, siteType[sitePosition],
                order_code,
                listToString(contract_imgs),
                listToString(survey_form_imgs),
                listToString(permit_imgs),
                card_front_imgs,
                card_back_imgs,
                contractType[contractTypePosition].id,
                handsel_price,
                listToString(agent_license_imgs),
                agency_name,
                object : GetBase {
                    override fun getInfo(base: BaseBean) {
                        uploadSuccess.value = base
                    }

                    override fun onError(e: String) {
                    }
                })
    }

    fun completionContract(order_code: String) {
        materialModel.completionContract(id,pact_id, order_code, listToString(contract_imgs), object : GetBase {
            override fun getInfo(base: BaseBean) {
                completionContractSuccess.value = base
            }

            override fun onError(e: String) {
            }
        })
    }
}